
package com.hengtiansoft.lfy.db;

import android.content.Context;


import com.hengtiansoft.lfy.been.ChangeWords;
import com.hengtiansoft.lfy.been.Child;
import com.hengtiansoft.lfy.been.ChildType;

import java.util.List;

/**
 * 场景数据库操作
 */
public class CL_InviteMessgeDao {


    public CL_InviteMessgeDao(Context context) {
        CL_DBManager.getInstance().onInit(context);
    }
   /* //获取要更新的    本地datetime       先取本地db数据  然后获取server数据 比较是否要更新  删除然后写入数据并且更新needUPdate字段  废弃
    public static List<String> updateChild_idList=new ArrayList<String>();
    public void getUpdateList(List<Child> serverChildList,List<Child> dbChildList){ //是有 ser和db比
        updateChild_idList.clear();
       for(Child serverChild:serverChildList){
           boolean hasChild=false;
           for(Child dbChild:dbChildList){
               if(serverChild.getChildId().equals(dbChild.getChildId())){
                   hasChild=true;
                   if(!serverChild.getDatatime().equals(dbChild.getDatatime())){
                       updateChild_idList.add(serverChild.getChildId()+"");
                   }
                   break;
               }
           }
           if(!hasChild){
               updateChild_idList.add(serverChild.getChildId()+"");
           }
       }
    }*/
    //获取要更新的 直接更新serverchild isneed字段 然后刷入数据库
    public void getUpdateListIsNeed(List<Child> serverChildList, List<Child> dbChildList){ //是有 ser和db比
        for(Child serverChild:serverChildList){
            boolean hasChild=false;
            for(Child dbChild:dbChildList){
                if(serverChild.getId().equals(dbChild.getId())){
                    hasChild=true;
                    serverChild.setDownload_type(dbChild.getDownload_type());
                    if(!serverChild.getCreate_time().equals(dbChild.getDownload_type())&&!"0".equals(dbChild.getDownload_type())){
                        serverChild.setDownload_type("2");
                    }
                    if(!serverChild.getCreate_time().equals(dbChild.getCreate_time())){
                       serverChild.setIsneedUpdate("1");

                    }else{
                        if("1".equals(dbChild.getIsneedUpdate())){
                            serverChild.setIsneedUpdate("1");
                        }
                    }
                    break;
                }
            }
            if(!hasChild){
                serverChild.setIsneedUpdate("1");
                serverChild.setDownload_type("0");
            }
        }
    }
    //更新收藏状态
   public void upDateCollect(String id, String type, String serverId){
       CL_DBManager.getInstance().upDateCollect(id,type,serverId);
   }
    //删除收藏状态
    public void deleteCollect(String type, String serverId){
        CL_DBManager.getInstance().deleteCollect(type,serverId);
    }
    /**
     * 一级分类已下载
     * @param child_id
     */
    public void downloadOK(String child_id, String time){
        CL_DBManager.getInstance().downloadOK(child_id, time);
    }
    /**
     * 二级分类已更新
     * @param childid
     * @param child_fen_id
     */
    public void upDatechildtype_isNeed_off(String childid, String child_fen_id){
        CL_DBManager.getInstance().upDatechildtype_isNeed_off(childid, child_fen_id);
    }

    /**
     * 一级分类已更新
     * @param childid
     */
    public void upDateChildCategroy_isNeed_off(String childid){
        CL_DBManager.getInstance().upDateChildCategroy_isNeed_off(childid);
    }
    /**
     * 保存场景
     * @param childList
     */
   public void saveChildCategroy(int categroyId, List<Child> childList){
       CL_DBManager.getInstance().saveChildCategroy(categroyId, childList);
   }

    /**
     * 获取所有场景分类
     * @param categroyId
     * @return
     */
    public List<Child> getChildByCategroyId(Integer categroyId){
        return CL_DBManager.getInstance().getChildByCategroyId(categroyId);
    }

    public List<Child> getChildDownloadByCategroyId(Integer categroyId){
        return CL_DBManager.getInstance().getChildDownloadByCategroyId(categroyId);
    }

    /**
     *保存分类
     * @param childTypeList
     */
    public void saveChildType(List<ChildType> childTypeList, String childid){
        CL_DBManager.getInstance().saveChildType(childTypeList,childid);
    }

    /**
     * 通过child_id查询所有分类
     * @param child_id
     * @return
     */
    public List<ChildType> getChildType(String child_id){
        return CL_DBManager.getInstance().getChildType(child_id);
    }

    /**
     * 查询words
     * @param child_id
     * @param childtype_id
     * @param chinese
     * @param english
     * @param page
     * @return
     */
    public List<ChangeWords> getSimilarResult(String child_id, String childtype_id, String chinese,
                                              String english, int page){
        return CL_DBManager.getInstance().getSimilarResult(child_id, childtype_id, chinese, english, page);
    }

    /**
     * 保存words
     * @param wordsList
     * @param childid
     */
    public void saveWords(List<ChangeWords> wordsList, String childid, String child_fen_id){
        CL_DBManager.getInstance().saveWords(wordsList,childid,child_fen_id);
    }
    /**
     * 保存words
     */
    public void saveWordsNew(List<ChangeWords> wordsList, String childid, String child_fen_id){
        List<ChangeWords> dbwordsList=CL_DBManager.getInstance().searchWords(childid, child_fen_id);
        if(dbwordsList!=null&&dbwordsList.size()>0){
            for(ChangeWords changeWords:dbwordsList){
                for(ChangeWords changeWordstemp:wordsList){
                    if(changeWords.getId().equals(changeWordstemp.getId())){
                        changeWordstemp.setCollect_server_id(changeWords.getCollect_server_id());
                        changeWordstemp.setWords_iscollect(changeWords.getWords_iscollect());
                    }
                }
            }
        }else{
            saveWords(wordsList,childid,child_fen_id);
        }
    }

    /**
     * 修改words
     * @param wordspath
     * @param words_id
     */
    public void updateWords(String wordspath, String words_id){
        CL_DBManager.getInstance().updateWords(wordspath,words_id);
    }

    /**
     * 批量修改words
     * @param changeWordsList
     */
    public void updateALLWords(List<ChangeWords> changeWordsList){
        CL_DBManager.getInstance().updateALLWords(changeWordsList);
    }
}
